#!/usr/bin/perl -w
#
# $Id: BoomBox.pm,v 1.5 2003/07/23 04:23:39 solovam Exp $
#
# This file is a part of gkismet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#
# BoomBox class
#
package BoomBox;

use Misc;
use ReusableConnObserver;
use strict;
@BoomBox::ISA = qw(ReusableConnObserver);

my $timeout = 3;

sub new
{
	my $type = shift;
	my $self = new ReusableConnObserver(@_);
	bless $self, $type;

	$self->{'nNetworks'} = 0;
	$self->{'gpsLock'} = 0;
	$self->{'packets'} = 0;
	$self->{'dropped'} = 0;
	$self->{'lastNetworkSound'} = 0;
	$self->{'lastGpsSound'} = 0;
	$self->{'lastTrafficSound'} = 0;
	$self->{'lastAlertSound'} = 0;

	return $self;
}

sub flush
{
	my $self = shift;

	$self->{'nNetworks'} = 0;
	$self->{'gpsLock'} = 0;
	$self->{'packets'} = 0;
	$self->{'dropped'} = 0;
	$self->{'lastNetworkSound'} = 0;
	$self->{'lastGpsSound'} = 0;
	$self->{'lastTrafficSound'} = 0;
	$self->{'lastAlertSound'} = 0;
}

sub update
{
	my $self = shift;
	my $object = shift;
	my $data = shift;

	if($object->isa('Connection') && defined $self->{'connection'} && $self->{'connection'} eq $object)
	{
		if($data->{'changed'} eq 'network')
		{
			my @networks = keys %{$self->{'connection'}->{'network'}};
			my $newNNetworks = $#networks + 1;
			if($newNNetworks > $self->{'nNetworks'})
			{
				if(time() - $self->{'lastNetworkSound'} > $timeout &&
					$self->{'gKismetApplication'}->{'preferences'}->getPref('networkSoundEnabled'))
				{
					$self->play($self->{'gKismetApplication'}->{'preferences'}->getPref('newNetworkSound'));
					$self->{'lastNetworkSound'} = time();
				}
			}
			$self->{'nNetworks'} = $newNNetworks;
		}

		if($data->{'changed'} eq 'gps' && defined $self->{'connection'}->{'gps'}->{'fix'})
		{
			my $newGpsLock = $self->{'connection'}->{'gps'}->{'fix'};
			if(time() - $self->{'lastGpsSound'} > $timeout &&
				$self->{'gKismetApplication'}->{'preferences'}->getPref('gpsSoundEnabled'))
			{
				if($newGpsLock  >= 2 && $self->{'gpsLock'} < 2)
				{
					$self->play($self->{'gKismetApplication'}->{'preferences'}->getPref('gpsAcqSound'));
					$self->{'lastGpsSound'} = time();
				}
				elsif($newGpsLock  < 2 && $self->{'gpsLock'} >= 2)
				{
					$self->play($self->{'gKismetApplication'}->{'preferences'}->getPref('gpsLostSound'));
					$self->{'lastGpsSound'} = time();
				}
			}
			$self->{'gpsLock'} = $newGpsLock;
		}

		if($data->{'changed'} eq 'info' && defined $self->{'connection'}->{'info'}->{'packets'} && defined $self->{'connection'}->{'info'}->{'dropped'})
		{
			my $newPackets = $self->{'connection'}->{'info'}->{'packets'};
			my $newDropped = $self->{'connection'}->{'info'}->{'dropped'};
			if(time() - $self->{'lastTrafficSound'} > $timeout &&
				$self->{'gKismetApplication'}->{'preferences'}->getPref('trafficSoundEnabled'))
			{
				my $deltaPackets = $newPackets - $self->{'packets'};
				my $deltaDropped = $newDropped - $self->{'dropped'};
				if($deltaPackets > 0)
				{
					if($deltaPackets > $deltaDropped)
					{
						$self->play($self->{'gKismetApplication'}->{'preferences'}->getPref('trafficSound'));
						$self->{'lastTrafficSound'} = time();
					}
					else
					{
						$self->play($self->{'gKismetApplication'}->{'preferences'}->getPref('junkTrafficSound'));
						$self->{'lastTrafficSound'} = time();
					}
				}
			}
			$self->{'packets'} = $newPackets;
			$self->{'dropped'} = $newDropped;
		}

		if($data->{'changed'} eq 'alert')
		{
			if(time() - $self->{'lastAlertSound'} > $timeout &&
				$self->{'gKismetApplication'}->{'preferences'}->getPref('alertSoundEnabled'))
			{
				$self->play($self->{'gKismetApplication'}->{'preferences'}->getPref('alertSound'));
				$self->{'lastAlertSound'} = time();
			}
		}
        }
}

sub play
{
	my $self = shift;
	my $sound = shift;
	my $playBinary = $self->{'gKismetApplication'}->{'preferences'}->getPref('playBinary');
	system("$playBinary $sound >/dev/null 2>&1 &");
}

1;
